package com.graden.background.service.sysmenu.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graden.background.repository.sysmenu.SysMenuMapper;
import com.graden.background.repository.sysmenu.entity.SysMenu;
import com.graden.background.repository.sysmenu.entity.SysMenuNode;
import com.graden.background.repository.sysmenufunction.entity.SysMenuFunction;
import com.graden.background.service.sysmenu.ISysMenuService;
import com.graden.background.service.sysmenufunction.ISysMenuFunctionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 系统菜单 服务实现类
 * </p>
 *
 * @author zjf
 * @since 2019-03-21
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

	@Resource
	SysMenuMapper sysMenuMapper;

	@Resource
	ISysMenuFunctionService iSysMenuFunctionService;

	/**
	 * 树形结构的方式获取菜单（全部）
	 */
	@Override
	public List<SysMenuNode> getMenuNodeTree() {
		return sysMenuMapper.getNodeTree();
	}

	/**
	 * 树形结构的方式获取菜单(隐藏不可见部分)
	 */
	@Override
	public List<SysMenuNode> getMenuNodeTreeByOnlyShow() {
		return sysMenuMapper.getNodeTreeByOnlyShow();
	}

	/**
	 * 树形结构的方式获取权限
	 */
	@Override
	public List<SysMenuNode> getTreeFunctions() {
		List<SysMenuNode> sysMenuNodes = getMenuNodeTree();
		setFunctionToRecursion(true, null, sysMenuNodes);
		return cleanUpTreeFunctions(sysMenuNodes);
	}


	/**
	 * 递归查找权限
	 */
	private void setFunctionToRecursion(boolean isFirst, Integer superId, List<SysMenuNode> sysMenuNodes) {
		for (SysMenuNode item : sysMenuNodes) {

			if (isFirst) {
				superId = item.getId();
			}

			item.setSuperId(superId);

			List<SysMenuFunction> sysMenuFunctions = iSysMenuFunctionService.getFunctionsByMenuId(item.getId());
			if (!sysMenuFunctions.isEmpty()) {
				item.setFunctions(sysMenuFunctions);
			}
			setFunctionToRecursion(false, superId, item.getNext());
		}
	}

	/**
	 * 按页面要求整理数据格式
	 */
	private List<SysMenuNode> cleanUpTreeFunctions(List<SysMenuNode> targetMenuNode) {
		List<SysMenuNode> cleanUpMenuNodes = new ArrayList<>();

		//拿到第一层数据
		for (SysMenuNode item : targetMenuNode) {
			SysMenuNode oneMenuNode = new SysMenuNode();
			oneMenuNode.setName("全选");
			oneMenuNode.setNext(new ArrayList<>());
			oneMenuNode.setDesc(item.getName());
			oneMenuNode.setId(item.getId());
			cleanUpMenuNodes.add(oneMenuNode);
		}
		cleanUpFunctionToRecursion(cleanUpMenuNodes, targetMenuNode);

		Iterator<SysMenuNode> it = cleanUpMenuNodes.iterator();
		while (it.hasNext()) {
			SysMenuNode item = it.next();
			if ((Objects.isNull(item.getNext()) || item.getNext().isEmpty())
					&& (Objects.isNull(item.getFunctions()) || item.getFunctions().isEmpty())) {
				it.remove();
			}
		}

		return cleanUpMenuNodes;
	}

	/**
	 * 递归把符合数据格式抽到新的对象
	 */
	private void cleanUpFunctionToRecursion(List<SysMenuNode> cleanUpMenuNodes, List<SysMenuNode> targetMenuNode) {
		for (SysMenuNode item : targetMenuNode) {

			SysMenuNode sysMenuNode = new SysMenuNode();
			sysMenuNode.setName(item.getName());
			sysMenuNode.setFunctions(item.getFunctions());
			sysMenuNode.setSuperId(item.getSuperId());
			sysMenuNode.setDesc(StringUtils.isEmpty(item.getDesc()) ? item.getName() : item.getDesc());

			if (!item.getNext().isEmpty()) {
				for (SysMenuNode next : item.getNext()) {
					StringBuffer stringBuffer = new StringBuffer();
					String desc = stringBuffer.append(item.getName()).append("/").append(next.getName()).toString();
					next.setDesc(desc);
				}
			}

			if (!Objects.isNull(item.getFunctions()) && !item.getFunctions().isEmpty()) {

				for (int i = 0; i < cleanUpMenuNodes.size(); i++) {
					if (cleanUpMenuNodes.get(i).getId() == sysMenuNode.getSuperId()) {
						cleanUpMenuNodes.get(i).getNext().add(sysMenuNode);
						break;
					}
				}
			}

			cleanUpFunctionToRecursion(cleanUpMenuNodes, item.getNext());
		}

	}
}
